package com.leoandroid.tool.toolsbox.new

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.room.Room
import com.leoandroid.tool.toolsbox.bean.NotesDbInfo
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityAddNotesBinding

/**
 *添加笔记
 */
class AddNotesActivity : AppCompatActivity() {

    private var dataBase: com.leoandroid.tool.toolsbox.bean.AppDataBase? = null
    private var dataBind: ActivityAddNotesBinding? = null
    private var keyId:Long = 0
    private var infoForKey: NotesDbInfo? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBind = ActivityAddNotesBinding.inflate(layoutInflater)
        setContentView(dataBind?.root)
//        dataBind?.addNotesBack?.setOnClickListener {
//            finish()
//        }
        initView()
    }

    companion object{
       fun startAddNotesActivity(context: Context,keyId:Long){
            val intent = Intent(context,AddNotesActivity::class.java)
            intent.putExtra("keyId",keyId)
            context.startActivity(intent)
       }
    }

    private fun initView(){
        dataBase = Room.databaseBuilder(applicationContext, com.leoandroid.tool.toolsbox.bean.AppDataBase::class.java,"notes-location-db").build()
        keyId = intent.getLongExtra("keyId",keyId)
        if (keyId>0){
            dataBind?.addNotesBt!!.text = "修改笔记"
            dataBind?.notesDelete!!.visibility = View.VISIBLE

            Thread{
                run {
                      infoForKey = dataBase?.notesInfo!!.getInfoForKey(keyId)
                    dataBind?.addNotesTitle?.setText(infoForKey?.notesTitle)
                    dataBind?.addNotesContent?.setText(infoForKey?.notesContent)
                }
            }.start()


        }



        dataBind?.apply {
            addNotesBack.setOnClickListener {
            finish()
        }
            notesDelete.setOnClickListener {
                Thread{
                    run {
                        if (infoForKey!=null){
                            dataBase?.notesInfo?.deleteNotes(infoForKey!!)
                            this@AddNotesActivity.runOnUiThread {
                                Toast.makeText(this@AddNotesActivity,"删除成功",Toast.LENGTH_SHORT).show()
                                finish()
                            }
                        }
                    }
                }.start()


            }



            addNotesBt.setOnClickListener {
                if (TextUtils.isEmpty(addNotesTitle.text.toString())){
                    Toast.makeText(this@AddNotesActivity,"请输入标题",Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                if (TextUtils.isEmpty(addNotesContent.text.toString())){
                    Toast.makeText(this@AddNotesActivity,"请输入笔记内容",Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }

                val noteInfo = NotesDbInfo()
                noteInfo.notesTitle = addNotesTitle.text.toString()
                noteInfo.notesContent = addNotesContent.text.toString()
                if (keyId>0){
                    noteInfo.uid = keyId
                }else{
                    noteInfo.uid = System.currentTimeMillis()
                }
                Thread{
                    run {
                        if(keyId>0){
                            dataBase?.notesInfo?.updateNotes(noteInfo)
                        }else{
                            dataBase?.notesInfo?.insertNotes(noteInfo)
                        }
                    }
                }.start()

                Toast.makeText(this@AddNotesActivity,"保存成功",Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }
}